<?php
namespace App\Controllers\Admin;

use App\Controllers\Controller;
use App\Logics\AdminRoleLogic;
use App\Enums\PageEnums;
use App\Logics\MenuLogic;
use App\Utils\PageBar;

class AdminRoleController extends AdminBaseController {

    private $logic;

    public function __construct(){
        parent::__construct();
        $this->logic = AdminRoleLogic::getInstance();
    }

    public function index(){
        $page = \request()->input('page', 1);
        $pageSize = PageEnums::PAGE_SIZE;
        $result = $this->logic->getPageList($page, $pageSize);
        $pageBar = new PageBar();
        $data = [
            'list' => $result['list'],
            'pageBar' => $pageBar->show($result['total'], $pageSize, $page)
        ];

        $this->view(__FUNCTION__, $data);
    }

    public function info(){
        $id = input('id', 0);
        $data = $this->logic->getDetail($id);
        $menuLogic = new MenuLogic();
        $menu = $menuLogic->getTreeList();
        //print_r($menu);exit;
        $this->view('info', ['id'=>$id, 'info' => $data, 'menu' => $menu]);
    }

    public function save(){
        $data = $this->request->all();
        $id = $data['id'] ?? 0;
        $this->logic->save($id, $data);
        $message = $id == 0 ? '添加成功' : '修改成功';
        $this->success([], $message);
    }

    public function delete(){
        $id = I('get.id', 0);
        try {
            $this->logic->delete($id);
            $this->success();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 设置上下架状态
     *
     * @author lichunguang 153102250@qq.com
     * @since 2022/4/27 下午2:25
     */
    public function setStatus(){
        $this->success();
    }

}